<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="座位编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.seatNum`" :rules="formRules.seatNum" class="mb-0px!">
            <el-input v-model="row.seatNum" placeholder="请输入座位编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="座位状态" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.seatStatus`" :rules="formRules.seatStatus" class="mb-0px!">
            <el-select v-model="row.seatStatus" placeholder="请选择座位状态">
                <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.SEAT_STATUS)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="座位名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.seatName`" :rules="formRules.seatName" class="mb-0px!">
            <el-input v-model="row.seatName" placeholder="请输入座位名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="座位位置" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.seatSite`" :rules="formRules.seatSite" class="mb-0px!">
            <el-input v-model="row.seatSite" placeholder="请输入座位位置" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="所属区域" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.seatRegion`" :rules="formRules.seatRegion" class="mb-0px!">
            <el-input v-model="row.seatRegion" placeholder="请输入所属区域" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加办公室座位信息</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { OfficeinfoApi } from '@/api/system/officeinfo'

const props = defineProps<{
  officeId: undefined // 办公室ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  officeId: [{ required: true, message: '办公室ID不能为空', trigger: 'blur' }],
  seatNum: [{ required: true, message: '座位编号不能为空', trigger: 'blur' }],
  seatName: [{ required: true, message: '座位名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.officeId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await OfficeinfoApi.getSeatinfoListByOfficeId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    officeId: undefined,
    seatNum: undefined,
    seatStatus: undefined,
    seatName: undefined,
    seatSite: undefined,
    id: undefined,
    seatRegion: undefined
  }
  row.officeId = props.officeId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>